package strategy

import (
	"encoding/json"
	"fmt"
	"github.com/Exleryz/gorequest"
	"github.com/Exleryz/m3u8/util"
)

// 自行校验 接口约束
var _ M3U8KeyOpt = (*ZSXQStrategy)(nil)

// 数据生成
type ZSXQStrategy struct {
}

func (strategy ZSXQStrategy) GetKeyBytes(uri string) ([]byte, error) {
	_, data, errors := util.NewRequest(gorequest.New().Get(uri)).
		AppendHeader("Cookie",
			"zsxq_access_token=;").
		EndBytes()
	if errors != nil && len(errors) > 0 {
		fmt.Println(errors)
		return nil, errors[0]
	}
	return data, nil
}

// https://api.zsxq.com/v2/groups/88885815888252/columns
// https://api.zsxq.com/v2/groups/88885815888252/columns/2444425411/topics?count=100&sort=attached_to_column_time&direction=desc

func ReqColumns() *Columns {
	_, body, errors := NewRequest("https://api.zsxq.com/v2/groups/88885815888252/columns").EndBytes()
	if errors != nil && len(errors) > 0 {
		fmt.Println(errors)
		return nil
	}
	columns := &Columns{}
	json.Unmarshal(body, columns)
	fmt.Println(columns)

	return columns
}

func ReqTopics(columnId int) *Topics {
	_, body, errors := NewRequest(fmt.Sprintf("https://api.zsxq.com/v2/groups/88885815888252/columns/%d/topics?count=100&sort=attached_to_column_time&direction=desc", columnId)).EndBytes()
	if errors != nil && len(errors) > 0 {
		fmt.Println(errors)
		return nil
	}

	topics := &Topics{}
	json.Unmarshal(body, topics)
	fmt.Println(topics)

	return topics
}

func ReqTopicInfo(topicId int) *TopicResp {
	_, body, errors := NewRequest(fmt.Sprintf("https://api.zsxq.com/v2/topics/%d/info", topicId)).EndBytes()
	if errors != nil && len(errors) > 0 {
		fmt.Println(errors)
		return nil
	}

	topics := &TopicResp{}
	json.Unmarshal(body, topics)
	fmt.Println(topics)

	return topics
}

func ReqVideo(videoId int) *VideoResp {
	_, body, errors := NewRequest(fmt.Sprintf("https://api.zsxq.com/v2/videos/%d/url", videoId)).EndBytes()
	if errors != nil && len(errors) > 0 {
		fmt.Println(errors)
		return nil
	}

	video := &VideoResp{}
	json.Unmarshal(body, video)
	fmt.Println(video)

	return video
}

func NewRequest(reqUrl string) *gorequest.SuperAgent {
	return util.NewRequest(gorequest.New().Get(reqUrl)).
		AppendHeader("Cookie",
			"zsxq_access_token=;")
}

type Columns struct {
	Succeeded bool `json:"succeeded"`
	RespData  struct {
		Column []Column `json:"columns"`
	} `json:"resp_data"`
}

type Column struct {
	ColumnId   int    `json:"column_id"`
	Name       string `json:"name"`
	CoverUrl   string `json:"cover_url"`
	Statistics struct {
		TopicsCount int `json:"topics_count"`
	} `json:"statistics"`
	CreateTime          string `json:"create_time"`
	LastTopicAttachTime string `json:"last_topic_attach_time"`
}

type Topics struct {
	Succeeded bool `json:"succeeded"`
	RespData  struct {
		Topics []Topic `json:"topics"`
	} `json:"resp_data"`
}
type Topic struct {
	TopicId              int    `json:"topic_id"`
	Title                string `json:"title"`
	Text                 string `json:"text"`
	CreateTime           string `json:"create_time"`
	AttachedToColumnTime string `json:"attached_to_column_time"`
}

type TopicResp struct {
	Succeeded bool `json:"succeeded"`
	RespData  struct {
		Topic struct {
			TopicId int64 `json:"topic_id"`
			Group   struct {
				GroupId int64  `json:"group_id"`
				Name    string `json:"name"`
				Type    string `json:"type"`
			} `json:"group"`
			Type string `json:"type"`
			Talk struct {
				Owner struct {
					UserId      int64  `json:"user_id"`
					Name        string `json:"name"`
					AvatarUrl   string `json:"avatar_url"`
					Description string `json:"description"`
					Location    string `json:"location"`
				} `json:"owner"`
				Text  string `json:"text"`
				Video struct {
					VideoId  int `json:"video_id"`
					Size     int `json:"size"`
					Duration int `json:"duration"`
					Cover    struct {
						Width  int    `json:"width"`
						Height int    `json:"height"`
						Url    string `json:"url"`
					} `json:"cover"`
				} `json:"video"`
			} `json:"talk"`
			LatestLikes []struct {
				CreateTime string `json:"create_time"`
				Owner      struct {
					UserId    int64  `json:"user_id"`
					Name      string `json:"name"`
					AvatarUrl string `json:"avatar_url"`
				} `json:"owner"`
			} `json:"latest_likes"`
			Annotation    string `json:"annotation"`
			LikesCount    int    `json:"likes_count"`
			RewardsCount  int    `json:"rewards_count"`
			CommentsCount int    `json:"comments_count"`
			ReadingCount  int    `json:"reading_count"`
			ReadersCount  int    `json:"readers_count"`
			Digested      bool   `json:"digested"`
			Sticky        bool   `json:"sticky"`
			CreateTime    string `json:"create_time"`
			UserSpecific  struct {
				Liked      bool `json:"liked"`
				Subscribed bool `json:"subscribed"`
			} `json:"user_specific"`
			Title   string `json:"title"`
			Columns []struct {
				ColumnId int64  `json:"column_id"`
				Name     string `json:"name"`
			} `json:"columns"`
		} `json:"topic"`
		Type string `json:"type"`
	} `json:"resp_data"`
}

type VideoResp struct {
	Succeeded bool `json:"succeeded"`
	RespData  struct {
		Url string `json:"url"`
	} `json:"resp_data"`
}
